package main.Q401_500;

import java.util.ArrayList;
import java.util.List;

public class Q491_500 {
	public static void main(String[] args) {
		System.out.println("Question491：递增子序列");
		int[] nums=new int[]{4,6,7,7};
		System.out.println(new Question491().findSubsequences(nums));
		System.out.println("Question492：构造矩形");
		System.out.println("Question493：");
		System.out.println("Question494：目标和");
		System.out.println("Question495：提莫攻击");
		System.out.println("Question496：下一个更大元素Ⅰ");
		System.out.println("Question497：");
		System.out.println("Question498：对角线遍历");
		System.out.println("Question499：");
		System.out.println("Question500：键盘行");
	}
}

class Question491 {
	public List<Integer> path = new ArrayList<>();
	public List<List<Integer>> result = new ArrayList<>();
	public List<List<Integer>> findSubsequences(int[] nums) {
		process(nums,0);
		return result;
	}

	public void process(int[] nums, int start) {
		if (path.size() > 1) {
			result.add(new ArrayList<>(path));
		}
		int[] used = new int[201];
		for (int i = start; i < nums.length; i++) {
			if (!path.isEmpty() && nums[i] < path.get(path.size() - 1) || (used[nums[i] + 100] == 1)) continue;
			used[nums[i] + 100] = 1;
			path.add(nums[i]);
			process(nums, i + 1);
			path.remove(path.size() - 1);
		}
	}
}

class Question492 {
	public int[] constructRectangle(int area) {
		for (int i = (int) (Math.sqrt(area)); ; i--) {
			if (area % i == 0) return new int[]{area / i, i};
		}
	}
}

class Question494 {
	public int process(int[] nums, int curSum, int curIndex) {
		if (curIndex == nums.length && curSum == 0) return 1;//数组全部使用完且等于目标值
		if (curIndex == nums.length && curSum != 0) return 0;//数组全部使用完且不等于目标值
		int countAdd = process(nums, curSum - nums[curIndex], curIndex + 1);//＋当前数并进入下一个数
		int countDecline = process(nums, curSum + nums[curIndex], curIndex + 1);//-当前数并进入下一个数
		return countAdd + countDecline;//+-方式构成的总数
	}

	public int findTargetSumWays(int[] nums, int target) {
		return process(nums, target, 0);
	}
}

class Question495 {//未完成

	public int findPoisonedDuration(int[] timeSeries, int duration) {
		int totalTime = 0;
		for (int i = 1; i < timeSeries.length; i++) {
			if (timeSeries[i - 1] + duration >= timeSeries[i]) {

			}
		}
		return 1;
	}
}

class Question496 {
	public static int[] nextGreaterElement(int[] nums1, int[] nums2) {
		int[] result = new int[nums1.length];
		for (int i = 0; i < nums1.length; i++) {
			int nextMax = -1, index = Integer.MAX_VALUE;
			for (int j = 0; j < nums2.length; j++) {
				if (nums1[i] == nums2[j]) {
					index = j;
					break;
				}
			}
			while (index < nums2.length) {
				if (nums2[index] > nums1[i]) {
					nextMax = nums2[index];
					break;
				}
				index++;
			}
			result[i] = nextMax;
		}
		return result;
	}
}

class Question498 {
	public static int[] findDiagonalOrder(int[][] mat) {
		int row = mat.length, col = mat[0].length;
		int[] arr = new int[row * col];
		int leftRow = 0, leftCol = 0, rightRow = 0, rightCol = 0, direct = 1, index = 0;//direct为1是向上遍历，为2是向下遍历
		do {
			if (direct == 1) {
				for (int i = leftRow, j = leftCol; i >= rightRow && j <= rightCol; i--, j++) {
					arr[index] = mat[i][j];
					index++;
				}
				direct = 2;
			} else if (direct == 2) {
				for (int i = rightRow, j = rightCol; i <= leftRow && j >= leftCol; i++, j--) {
					arr[index] = mat[i][j];
					index++;
				}
				direct = 1;
			}
			if (leftRow != row - 1) leftRow++;//左是否变向
			else leftCol++;
			if (rightCol != col - 1) rightCol++;//右是否变向
			else rightRow++;
		} while (leftCol <= rightCol && rightRow <= leftRow);
		return arr;
	}
}

class Question500 {
	public String[] findWords(String[] words) {
		List<String> list = new ArrayList<String>();
		String rowIdx = "12210111011122000010020202";
		for (String word : words) {
			boolean isValid = true;
			char idx = rowIdx.charAt(Character.toLowerCase(word.charAt(0)) - 'a');
			for (int i = 1; i < word.length(); ++i) {
				if (rowIdx.charAt(Character.toLowerCase(word.charAt(i)) - 'a') != idx) {
					isValid = false;
					break;
				}
			}
			if (isValid) {
				list.add(word);
			}
		}
		String[] ans = new String[list.size()];
		for (int i = 0; i < list.size(); ++i) {
			ans[i] = list.get(i);
		}
		return ans;
	}
}